<script setup>
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useAllDataStore } from '@/stores';

// 初始化路由和 Store
const router = useRouter();
const route = useRoute();
const store = useAllDataStore();

// 计算属性
const noChildrenItems = computed(() => store.state.menuList.filter(item => !item.children));
const hasChildrenItems = computed(() => store.state.menuList.filter(item => item.children));
const isSidebarCollapsed = computed(() => store.state.isCollapse);
const sidebarWidth = computed(() => isSidebarCollapsed.value ? '64px' : '180px');

// 处理菜单点击事件
const navigateTo = (item) => {
  router.push(item.path);
  store.selectMenu(item);
};
</script>

<template>
  <el-aside :width="sidebarWidth">
    <el-menu
      background-color="#ffffff"
      text-color="#333333"
      active-text-color="#409EFF"
      :collapse="isSidebarCollapsed"
      :collapse-transition="false"
      :default-active="route.path"
    >
      <!-- Logo -->
      <div class="logo">
        <span v-show="!isSidebarCollapsed">瑾禾服装管理系统</span>
        <span v-show="isSidebarCollapsed">瑾禾</span>
      </div>

      <!-- 无子菜单项 -->
      <el-menu-item
        v-for="item in noChildrenItems"
        :index="item.path"
        :key="item.path"
        @click="navigateTo(item)"
      >
        <component class="icon" :is="item.icon" />
        <span>{{ item.label }}</span>
      </el-menu-item>

      <!-- 有子菜单项 -->
      <el-sub-menu
        v-for="item in hasChildrenItems"
        :index="item.path"
        :key="item.path"
      >
        <template #title>
          <component class="icon" :is="item.icon" />
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            v-for="subItem in item.children"
            :index="subItem.path"
            :key="subItem.path"
            @click="navigateTo(subItem)"
          >
            <component class="icon" :is="subItem.icon" />
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<style lang="less" scoped>
.el-aside {
  height: 100vh;
  background-color: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.el-menu {
  border: none;
  background-color: #ffffff;
}

.logo {
  height: 60px;
  line-height: 60px;
  color: #333333;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  background-color: #ffffff;
  border-bottom: 1px solid #f0f0f0;
}

.icon {
  width: 18px;
  height: 18px;
  margin-right: 10px;
  vertical-align: middle;
  color: #666666;
}

.el-menu-item,
.el-sub-menu__title {
  height: 50px;
  line-height: 50px;
  padding-left: 20px !important;
  color: #333333;
}

.el-menu-item:hover,
.el-sub-menu__title:hover {
  background-color: #f5f7fa;
  color: #409EFF;
  
  .icon {
    color: #409EFF;
  }
}

.el-menu-item.is-active {
  background-color: #ecf5ff;
  color: #409EFF;
  border-right: 3px solid #409EFF;
  
  .icon {
    color: #409EFF;
  }
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 40px !important;
    color: #666666;
    
    &:hover {
      color: #409EFF;
      background-color: #f5f7fa;
      
      .icon {
        color: #409EFF;
      }
    }
    
    &.is-active {
      color: #409EFF;
      background-color: #ecf5ff;
      
      .icon {
        color: #409EFF;
      }
    }
  }
}

:deep(.el-sub-menu__title) {
  &:hover {
    color: #409EFF;
    
    .icon {
      color: #409EFF;
    }
  }
}
</style>